using System;
using System.Text.RegularExpressions;
using FBroSharp.Const;
using FBroSharp.Callback;

namespace JavaScript对话框自动化处理器
{
    /// <summary>
    /// JavaScript对话框自动化处理器
    /// 支持自动处理alert、confirm、prompt对话框
    /// 支持批量响应规则（正则表达式匹配）
    /// </summary>
    public class JSDialogHandler
    {
        private readonly Form1 mainForm;
        private readonly int timeoutMs = 5000;
        private int dialogCount = 0;

        public JSDialogHandler(Form1 form)
        {
            mainForm = form;
        }

        /// <summary>
        /// 处理JavaScript对话框
        /// </summary>
        public bool HandleDialog(FBroJsDialogType dialogType, string message, string defaultPromptText, IFBroSharpJSDialogCallback callback)
        {
            try
            {
                dialogCount++;
                bool enableLogging = mainForm.IsLoggingEnabled();
                string autoResponse = mainForm.GetAutoResponse();
                string promptInput = mainForm.GetPromptInput();

                if (enableLogging)
                {
                    string typeStr = GetDialogTypeString(dialogType);
                    mainForm.AddLog($"检测到{typeStr}对话框: {message}");
                }

                // 首先尝试批量响应规则
                if (HandleBatchResponse(message, dialogType, callback, enableLogging))
                {
                    return true;
                }

                // 如果批量规则未匹配，使用默认处理
                bool success;
                string userInput = "";

                switch (autoResponse.ToLower())
                {
                    case "accept":
                        success = true;
                        userInput = dialogType == FBroJsDialogType.JSDIALOGTYPE_PROMPT ? 
                            (string.IsNullOrEmpty(promptInput) ? defaultPromptText : promptInput) : "";
                        break;
                    case "dismiss":
                        success = false;
                        userInput = "";
                        break;
                    case "custom":
                        // 自定义处理逻辑
                        success = true;
                        userInput = dialogType == FBroJsDialogType.JSDIALOGTYPE_PROMPT ? promptInput : "";
                        break;
                    default:
                        success = true;
                        userInput = dialogType == FBroJsDialogType.JSDIALOGTYPE_PROMPT ? defaultPromptText : "";
                        break;
                }

                // 执行回调
                callback.Continue(success, userInput);

                if (enableLogging)
                {
                    mainForm.AddLog($"响应: {(success ? "确定" : "取消")}" + 
                        (dialogType == FBroJsDialogType.JSDIALOGTYPE_PROMPT ? $", 输入: {userInput}" : ""));
                    mainForm.UpdateDialogStatistics(dialogCount);
                }

                return true; // 表示已处理对话框
            }
            catch (Exception ex)
            {
                mainForm.AddLog($"对话框处理异常: {ex.Message}");
                // 异常时默认接受
                callback.Continue(true, defaultPromptText);
                return true;
            }
        }

        /// <summary>
        /// 批量响应规则处理
        /// </summary>
        private bool HandleBatchResponse(string message, FBroJsDialogType dialogType, IFBroSharpJSDialogCallback callback, bool enableLogging)
        {
            try
            {
                // 规则 1: 确认删除
                if (Regex.IsMatch(message, @"确认删除"))
                {
                    callback.Continue(true, "");
                    if (enableLogging)
                    {
                        mainForm.AddLog("批量规则匹配: 确认删除 -> 自动确定");
                    }
                    return true;
                }

                // 规则 2: 取消操作
                if (Regex.IsMatch(message, @"取消操作"))
                {
                    callback.Continue(false, "");
                    if (enableLogging)
                    {
                        mainForm.AddLog("批量规则匹配: 取消操作 -> 自动取消");
                    }
                    return true;
                }

                // 规则 3: 请输入.*
                if (Regex.IsMatch(message, @"请输入.*"))
                {
                    callback.Continue(true, "批量输入内容");
                    if (enableLogging)
                    {
                        mainForm.AddLog("批量规则匹配: 请输入 -> 自动输入'批量输入内容'");
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                if (enableLogging)
                {
                    Console.WriteLine($"批量规则处理异常: {ex.Message}");
                }
            }
            return false; // 未匹配任何规则
        }

        /// <summary>
        /// 获取对话框类型字符串
        /// </summary>
        private string GetDialogTypeString(FBroJsDialogType dialogType)
        {
            switch (dialogType)
            {
                case FBroJsDialogType.JSDIALOGTYPE_ALERT:
                    return "Alert";
                case FBroJsDialogType.JSDIALOGTYPE_CONFIRM:
                    return "Confirm";
                case FBroJsDialogType.JSDIALOGTYPE_PROMPT:
                    return "Prompt";
                default:
                    return "Unknown";
            }
        }
    }
}

